package com.uw.uwregistry.service;

import com.uw.uwregistry.meta.InstanceMeta;
import com.uw.uwregistry.meta.ServerMeta;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RegistryContainer {

    // 注册的服务容器
    private static final MultiValueMap<String, InstanceMeta> container = new LinkedMultiValueMap<>();

    private static final Map<String, Long> heartbeatMap = new HashMap<>();

    private static final Map<String, Long> versionMap = new HashMap<>();

    private static long containerVersion = -1L;

    private static ServerMeta serverMeta;



    public static synchronized List<InstanceMeta> getIntanceList(String key) {
        return container.get(key);
    }

    public static synchronized void addInstance(String key, InstanceMeta instanceMeta) {
        container.add(key, instanceMeta);
    }

    public static synchronized void putVersionMap(String key, Long value) {
        versionMap.put(key, value);
    }

    public static synchronized void putHeartbeatMao(String key, Long value) {
        heartbeatMap.put(key, value);
    }

    public static synchronized void removeHeartbeatMap(String key) {
        heartbeatMap.remove(key);
    }

    public static Map<String, Long> heartbeatMap() {
        return snapshot().getHeartbeatMap();
    }

    public static Map<String, Long> versionMap() {
        return snapshot().getVersionMap();
    }


    public static void updateContainerVersion(long version) {
        containerVersion = version;
    }

    public static long containerVersion() {
        return containerVersion;
    }


    public static synchronized long restore(Snapshot snapshot) {
        container.clear();
        container.addAll(snapshot.getContainer());

        versionMap.clear();
        versionMap.putAll(snapshot.getVersionMap());

        heartbeatMap.clear();
        heartbeatMap.putAll(snapshot.getHeartbeatMap());

        containerVersion = snapshot.getVersion();
        return snapshot.getVersion();
    }

    public static synchronized Snapshot snapshot() {
        LinkedMultiValueMap<String, InstanceMeta> container = new LinkedMultiValueMap<>();
        container.addAll(RegistryContainer.container);

        return Snapshot.builder()
                .container(container)
                .heartbeatMap(new HashMap<>(heartbeatMap))
                .versionMap(new HashMap<>(versionMap))
                .version(RegistryContainer.containerVersion())
                .build();

    }

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class Snapshot {

        private LinkedMultiValueMap<String, InstanceMeta> container = new LinkedMultiValueMap<>();

        private Map<String, Long> heartbeatMap = new HashMap<>();

        private Map<String, Long> versionMap = new HashMap<>();

        private Long version = -1L;


    }
}
